import {defineConfig, loadEnv} from 'vite'
import vue from '@vitejs/plugin-vue'
import path from 'path'
//引入svg需要用到的插件
import {createSvgIconsPlugin} from "vite-plugin-svg-icons";
//mock插件
//@ts-ignore
import {viteMockServe} from 'vite-plugin-mock'
// https://vitejs.dev/config/
export default defineConfig(({command, mode}) => {
    //获取环境变量
    let env = loadEnv(mode, process.cwd());
    return {
        plugins: [
            vue(),
            createSvgIconsPlugin({
                iconDirs: [path.resolve(process.cwd(), 'src/assets/icons')],
                symbolId: 'icon-[dir]-[name]',
            }),
            viteMockServe({
                localEnabled: command === 'serve',//保证开发阶段可以使用mock的接口
            })
        ],
        resolve: {
            alias: {
                "@": path.resolve("./src") //给src路径起别名
            }
        },
        css: {
            preprocessorOptions: {
                scss: {
                    javascriptEnabled: true,
                    additionalData: '@import "./src/styles/variable.scss";',
                }
            }
        },
        //代理跨域
        server: {
            proxy: {
                [env.VITE_APP_BASE_API]: {
                    target: env.VITE_SERVE, //获取数据的服务器地址
                    changeOrigin: true, //需要代理跨域
                    rewrite: (path) => path.replace(/^\/api/, ''),//路径重写  把/api-->替换成/
                }

            }
        }
    }
})
